package com.Cychat.chat.controller;

import com.Cychat.chat.service.ChatOssSevice;
import com.Cychat.commons.entity.ResultMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * Auther: 刘博文
 * Date: 2025/11/4:14:21
 * Description: 聊天控制器
 }
 */
@Controller
@RequestMapping("/api/chat")
@Slf4j
public class ChatController {
    @Autowired
    private ChatOssSevice chatOssSevice;
    /**
     * Description : 接收用户上传的图片返回图片URL
     * ChangeLog : 1. 创建 (2025/11/4 14:23 [Esang]);
     * @param file
     * @return rul
     */
    @PostMapping("/upload/{userId}")
    @ResponseBody
    public ResultMessage <String> upload(@RequestParam("file") MultipartFile file,
                                         @PathVariable("userId") Long userId) {
        try {
            String uploadimagesUrl = chatOssSevice.uploadimages(file, userId);
            if (uploadimagesUrl != null){
                return  ResultMessage.success(uploadimagesUrl);
            }
            return ResultMessage.error("发送图片失败");
        } catch (IOException e) {
            log.error("上传图片异常：", e);
        }
        return ResultMessage.error("发送图片失败");
    }
}
